﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BDCS.BLL;
using BDCS.Model;

namespace BDCS.UI
{
    public partial class AddDefineSelect : Form
    {
        private int btid;
        private int sbtid;
        private int dtid;
        private int dtidTwo;
        private int dstid;
        private int parentDsitd;
        private int dstidTwo;
        private int sbaid;
        private int said;
        private int dcid;
        private string sbdcid;
        private DataSet dataSet;
        private int selectCount;
        private int isSelect;
        private bool isAdd;
        private string s1;
        private string s2;
        private string s3;
        private BDCS.Common.AddResult selectResult;
        private BDCS.BLL.T_SubAttributes bllSubAttribute;
        private BDCS.BLL.T_SubBuildAttributes bllSubBuildAttribute;
        private BDCS.BLL.T_BuildDesignDcriterionSubTypes bllBuildDesignDcriterionSubType;
        private BDCS.BLL.T_BuildDesignDcriterionContents bllBuildDesignDcriterionContent;
        private BDCS.BLL.T_BuildDesignDcriterionTypes bllBuildDesignDcriterionType;
        private BDCS.BLL.T_SubBuildDcriterionDesignAttributes bllSubBuildDcriterionDesignAttribute;
        private BDCS.BLL.T_SubBuildDcriterionContents bllSubBuildDcriterionContent;
        private BDCS.BLL.T_SubBuildDcriterionConditions bllSubBuildDcriterionCondition;
        private BDCS.BLL.T_SubBuildDcriterionList bllSubBuildDcriterionList;
        private BDCS.Model.T_SubAttributes modelSubAttribute;
        private List<Model.T_SubAttributes> modelListSubAttribute;
        private BDCS.BLL.SelectResult bllSelectResult;
        private BDCS.Model.T_BuildDesignDcriterionContents modelBuildDesignDcriterionContent;
        private List<Model.T_BuildDesignDcriterionContents> modelListBuildDesignDcriterionContent;
        private BDCS.Model.T_SubBuildDcriterionDesignAttributes modelSubBuildDcriterionDesignAttribute;
        private List<Model.AddSelectAttributes> ListAddSelectAttribute;
        private List<int> listDstid;
        private List<int> listDstidTwo;
        private List<string> listSbdcid;
        private List<int> listDcid;
        private List<int> listSbaid;
        private List<int> listSaid;
        private List<int> listDtid;
        private Form partentForm;
        private DataSet selectReusltDataSet;
        private List<BDCS.Common.AddResult> listResult;
        public AddDefineSelect()
        {
            InitializeComponent();
        }
        public AddDefineSelect(int btid, int sbtid, int dtid, int dstid)
        {
            this.btid = btid;
            this.sbtid = sbtid;
            this.dtid = dtid;
            this.parentDsitd = dstid;
            InitializeComponent();
        }
        private void AddDefineSelect_Load(object sender, EventArgs e)
        {
            modelListSubAttribute = new List<Model.T_SubAttributes>();
            modelListBuildDesignDcriterionContent = new List<Model.T_BuildDesignDcriterionContents>();
            listDstid = new List<int>();
            listSbdcid = new List<string>();
            listDcid = new List<int>();
            listSbaid = new List<int>();
            listSaid = new List<int>();
            listDtid = new List<int>();
            listDstidTwo = new List<int>();
            listResult = new List<Common.AddResult>();
            ListAddSelectAttribute = new List<AddSelectAttributes>();
            selectCount = 0;
            isSelect = 0;
            CmbSubBuildAttributeDataBinding(sbtid);
            CmbSubAttributeDataBinding(sbaid);
            CmbBuildSubDcriterionDataBinding(dtid);
            CmbcmbBuildDcriterionContent(dstid);
            //  CmbDcriterionDataBinding();
            CmbSubDcriterionTwoDataBinding(dtid);
        }
        private void CmbSubBuildAttributeDataBinding(int sbtid)
        {
            DataSet dataSet = new DataSet();
            dataSet = new DataSet();
            bllSubBuildAttribute = new BLL.T_SubBuildAttributes();
            dataSet = bllSubBuildAttribute.GetList("SBTID=" + "'" + sbtid + "'");
            cmbSubBuildAttribute.DataSource = dataSet.Tables[0];
            cmbSubBuildAttribute.DisplayMember = dataSet.Tables[0].Columns["SBAContent"].ToString(); ;
            cmbSubBuildAttribute.ValueMember = dataSet.Tables[0].Columns["SBAID"].ToString();
        }
        private void CmbSubAttributeDataBinding(int sbaid)
        {
            DataSet dataSet = new DataSet();
            bllSubAttribute = new BLL.T_SubAttributes();
            dataSet = bllSubAttribute.GetList("SBAID=" + "'" + sbaid + "'");
            if (dataSet.Tables[0].Rows.Count != 0)
            {
                cmbSubAttribute.DataSource = dataSet.Tables[0];
                cmbSubAttribute.DisplayMember = dataSet.Tables[0].Columns["SAContent"].ToString();
                cmbSubAttribute.ValueMember = dataSet.Tables[0].Columns["SAID"].ToString();
            }
            else
            {
                cmbSubAttribute.DataSource = null;
            }
        }
        private void CmbBuildSubDcriterionDataBinding(int dtid)
        {
            DataSet dataSet = new DataSet();
            bllBuildDesignDcriterionSubType = new BLL.T_BuildDesignDcriterionSubTypes();
            dataSet = bllBuildDesignDcriterionSubType.GetList("DTID=" + "'" + dtid + "'");
            //cmbSubDcriterion.DataSource = null;
            if (dataSet.Tables[0].Columns.Count != 0)
            {
                cmbSubDcriterion.DataSource = dataSet.Tables[0];
                cmbSubDcriterion.DisplayMember = dataSet.Tables[0].Columns["DSTName"].ToString();
                cmbSubDcriterion.ValueMember = dataSet.Tables[0].Columns["DSTID"].ToString();
            }
            else
            {
                cmbSubDcriterion.DataSource = null;
            }
        }
        private void CmbcmbBuildDcriterionContent(int dstid)
        {
            DataSet dataSet = new DataSet();
            bllBuildDesignDcriterionContent = new BLL.T_BuildDesignDcriterionContents();
            dataSet = bllBuildDesignDcriterionContent.GetList("DSTID=" + "'" + dstid + "'");
            if (dataSet.Tables[0].Rows.Count != 0)
            {
                cmbBuildDcriterionContent.DataSource = dataSet.Tables[0];
                cmbBuildDcriterionContent.DisplayMember = dataSet.Tables[0].Columns["DcriterionContent"].ToString();
                cmbBuildDcriterionContent.ValueMember = dataSet.Tables[0].Columns["DCID"].ToString();
            }
            else
            {
                cmbBuildDcriterionContent.DataSource = null;
            }
        }
        //private void CmbDcriterionDataBinding()
        //{
        //    DataSet dataSet = new DataSet();
        //    bllBuildDesignDcriterionType = new BLL.T_BuildDesignDcriterionTypes();
        //    dataSet = bllBuildDesignDcriterionType.GetAllList();
        //    cmbDcriterion.DataSource = dataSet.Tables[0];
        //    cmbDcriterion.DisplayMember = dataSet.Tables[0].Columns["DTName"].ToString();
        //    cmbDcriterion.ValueMember = dataSet.Tables[0].Columns["DTID"].ToString();
        //}
        private void CmbSubDcriterionTwoDataBinding(int dtid)
        {
            DataSet dataSet = new DataSet();
            bllBuildDesignDcriterionSubType = new BLL.T_BuildDesignDcriterionSubTypes();
            dataSet = bllBuildDesignDcriterionSubType.GetList("DTID=" + "'" + dtid + "'");
            //cmbSubDcriterion.DataSource = null;
            if (dataSet.Tables[0].Columns.Count != 0)
            {
                cmbSubDcriterionTwo.DataSource = dataSet.Tables[0];
                cmbSubDcriterionTwo.DisplayMember = dataSet.Tables[0].Columns["DSTName"].ToString();
                cmbSubDcriterionTwo.ValueMember = dataSet.Tables[0].Columns["DSTID"].ToString();
            }
            else
            {
                cmbSubDcriterionTwo.DataSource = null;
            }
        }
        private DataSet SelectResult(List<int> listSaid, List<int> listDcid, List<int> listSelectDstid)
        {
            bllSubBuildDcriterionDesignAttribute = new BLL.T_SubBuildDcriterionDesignAttributes();
            bllSubBuildDcriterionCondition = new BLL.T_SubBuildDcriterionConditions();
            bllSubBuildDcriterionDesignAttribute = new BLL.T_SubBuildDcriterionDesignAttributes();
            bllSubBuildDcriterionContent = new BLL.T_SubBuildDcriterionContents();
            bllSubBuildDcriterionList = new BLL.T_SubBuildDcriterionList();
            dataSet = new DataSet();
            foreach (int dstid in listSelectDstid)
            {
                //   dataSet.Clear();
                dataSet = bllSubBuildDcriterionList.GetList("SBTID=" + "'" + this.sbtid + "'" + " " + "and" + " " + "DSTID=" + "'" + dstid + "'");
                if (dataSet.Tables[0].Rows.Count != 0)
                {
                    foreach (DataRow dr in dataSet.Tables[0].Rows)
                    {

                        sbdcid = dr["SBDCID"].ToString();
                        if (listSaid.Count != 0 && listDcid.Count != 0)
                        {
                            DataSet condtionDataSet = new DataSet();
                            DataSet contentDataSet = new DataSet();
                            DataSet attributeDataSet = new DataSet();
                            attributeDataSet = bllSubBuildDcriterionDesignAttribute.GetList("SBDCID=" + "'" + sbdcid + "'");
                            condtionDataSet = bllSubBuildDcriterionCondition.GetList("SBDCID=" + "'" + sbdcid + "'");
                            foreach (int sa in listSaid)
                            {
                                foreach (DataRow dw in attributeDataSet.Tables[0].Rows)
                                {
                                    if (Convert.ToInt32(dw["SAID"]) == sa)
                                    {

                                        s1 = dw["SBDCID"].ToString();
                                        break;
                                    }
                                    else
                                    {
                                        s1 = "S1不存在";
                                    }
                                }

                            }
                            foreach (int sc in listDcid)
                            {
                                foreach (DataRow da in condtionDataSet.Tables[0].Rows)
                                {
                                    if (sc == Convert.ToInt32(da["DCID"]))
                                    {
                                        s2 = (da["SBDCID"]).ToString();
                                        break;
                                    }
                                    else
                                    {
                                        s2 = "S2不存在";
                                    }
                                }
                            }
                            if (s1 == s2)
                            {
                                bllSelectResult = new SelectResult();
                                dataSet = new DataSet();
                                dataSet = bllSelectResult.GetList("a.SBDCID=" + "'" + s1 + "'");
                            }
                            else
                            {
                                MessageBox.Show("没有找到符合条件的相应规范！");
                            }
                        }
                        else
                        {
                            if (listSaid.Count != 0)
                            {
                                DataSet attributeDataSet = new DataSet();
                                attributeDataSet = bllSubBuildDcriterionDesignAttribute.GetList("SBDCID=" + "'" + sbdcid + "'");
                                foreach (int sa in listSaid)
                                {
                                    foreach (DataRow dw in attributeDataSet.Tables[0].Rows)
                                    {
                                        if (Convert.ToInt32(dw["SAID"]) == sa)
                                        {
                                            bllSubBuildDcriterionCondition = new BLL.T_SubBuildDcriterionConditions();
                                            dataSet = new DataSet();
                                            dataSet = bllSubBuildDcriterionCondition.GetList("SBDCID=" + "'" + dw["SBDCID"] + "'");
                                            if (dataSet.Tables[0].Rows.Count == 0)
                                            {
                                                s1 = dw["SBDCID"].ToString();
                                                break;
                                            }
                                            else
                                            {
                                                s1 = "S1不存在";
                                            }
                                        }
                                        else
                                        {
                                            s1 = "S1不存在";
                                        }
                                    }
                                }
                            }
                            if (s1 != "S1不存在")
                            {
                                isAdd = true;
                                bllSelectResult = new SelectResult();
                                dataSet = new DataSet();
                                dataSet = bllSelectResult.GetList("a.SBDCID=" + "'" + s1 + "'");
                            }
                            else
                            {
                                MessageBox.Show("没有找到符合条件的相应规范！");
                            }
                        }
                    }
                    return dataSet;
                }
                else
                {
                    dataSet = new DataSet();
                    dataSet = null;
                }
            }
            return dataSet;
        }
        private void cmbSubBuildAttribute_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                sbaid = Convert.ToInt32(cmbSubBuildAttribute.SelectedValue);
                CmbSubAttributeDataBinding(sbaid);
            }
            catch (Exception ex)
            {
                DataRowView drv = (DataRowView)cmbSubBuildAttribute.SelectedValue;
                sbaid = Convert.ToInt32(drv.Row["SBAID"]);
            }
        }

        private void cmbSubAttribute_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSubAttribute.DataSource != null)
            {
                try
                {
                    said = Convert.ToInt32(cmbSubAttribute.SelectedValue);
                    this.btnAddAttribute.Enabled = true;
                    this.btnAttributeCancel.Enabled = true;

                }
                catch (Exception ex)
                {
                    DataRowView drv = (DataRowView)cmbSubAttribute.SelectedValue;
                    said = Convert.ToInt32(drv.Row["SAID"]);
                    this.btnAddAttribute.Enabled = true;
                    this.btnAttributeCancel.Enabled = true;


                }
            }
            else
            {
                this.btnAddAttribute.Enabled = false;
                this.btnAttributeCancel.Enabled = false;

                MessageBox.Show("所选建筑属性无属性值，请从新选择！");
            }
        }

        private void btnAddAttribute_Click(object sender, EventArgs e)
        {
            if (modelListSubAttribute.Count >= 1)
            {
                bool isSelect = false;
                for (int i = 0; i < modelListSubAttribute.Count; i++)
                {
                    if (modelListSubAttribute[i].SAID == Convert.ToInt32(cmbSubAttribute.SelectedValue))
                    {
                        isSelect = true;
                        break;
                    }
                    else
                    {
                        isSelect = false;
                    }
                }
                if (isSelect == false)
                {
                    modelSubAttribute = new Model.T_SubAttributes();
                    bllSubAttribute = new BLL.T_SubAttributes();
                    modelSubAttribute = bllSubAttribute.GetModel(said);
                    Model.AddSelectAttributes modelSelectAttribute = new AddSelectAttributes();
                    DataRowView drv = (DataRowView)cmbSubBuildAttribute.SelectedItem;
                    DataRowView drv1 = (DataRowView)cmbSubAttribute.SelectedItem;
                    modelSelectAttribute.SelectSubAttribute = drv.Row["SBAContent"].ToString();
                    modelSelectAttribute.SelectAttribute = drv1.Row["SAContent"].ToString();
                    ListAddSelectAttribute.Add(modelSelectAttribute);
                    BDCS.Common.AddResult.selectAttributeText = string.Empty;
                    foreach (Model.AddSelectAttributes a in ListAddSelectAttribute)
                    {
                        BDCS.Common.AddResult.selectAttributeText += a.SelectSubAttribute + "," + a.SelectAttribute + ";";
                    }
                    lblSelectAttribute.Text = "选择的建筑类别查询条件为：" + BDCS.Common.AddResult.selectAttributeText;
                    modelListSubAttribute.Add(modelSubAttribute);
                    listSbaid.Add(sbaid);
                }
                else
                {
                    DataRowView drv1 = (DataRowView)cmbSubBuildAttribute.SelectedItem;
                    DataRowView drv = (DataRowView)cmbSubAttribute.SelectedItem;
                    string selectAttribute = drv.Row["SAContent"].ToString();
                    string selectSubAttribute = drv1.Row["SBAContent"].ToString();
                    MessageBox.Show("所选参数:" + "建筑属性名=" + "'" + selectSubAttribute + "'" + "," + "建筑属性值=" + "'" + selectAttribute + "'" + "重复，请重新添加！");
                }
            }
            else
            {
                modelSubAttribute = new Model.T_SubAttributes();
                bllSubAttribute = new BLL.T_SubAttributes();
                modelSubAttribute = bllSubAttribute.GetModel(said);
                Model.AddSelectAttributes modelSelectAttribute = new AddSelectAttributes();
                DataRowView drv = (DataRowView)cmbSubBuildAttribute.SelectedItem;
                DataRowView drv1 = (DataRowView)cmbSubAttribute.SelectedItem;
                modelSelectAttribute.SelectSubAttribute = drv.Row["SBAContent"].ToString();
                modelSelectAttribute.SelectAttribute = drv1.Row["SAContent"].ToString();
                ListAddSelectAttribute.Add(modelSelectAttribute);
                BDCS.Common.AddResult.selectAttributeText = string.Empty;
                BDCS.Common.AddResult.selectAttributeText = drv.Row["SBAContent"].ToString() + "," + drv1.Row["SAContent"].ToString() + ";";
                lblSelectAttribute.Text = "选择的建筑类别查询条件为：" + BDCS.Common.AddResult.selectAttributeText;
                modelListSubAttribute.Add(modelSubAttribute);
                listSbaid.Add(sbaid);
                listSaid.Add(said);
                isSelect++;
            }
        }

        private void btnAttributeCancel_Click(object sender, EventArgs e)
        {
            if (modelListSubAttribute.Count == 0)
            {
                lblSelectAttribute.Text = "选择的建筑类别查询条件为：";
                MessageBox.Show("没有添加任何属性查询条件，无需取消！");
            }
            else
            {
                    DataRowView drv1 = (DataRowView)cmbSubBuildAttribute.SelectedItem;
                    DataRowView drv = (DataRowView)cmbSubAttribute.SelectedItem;
                    string selectAttribute = drv.Row["SAContent"].ToString();
                    string selectSubAttribute = drv1.Row["SBAContent"].ToString();
                    for (int i = 0; i < modelListSubAttribute.Count; i++)
                    {
                        if (modelListSubAttribute[i].SAID == Convert.ToInt32(cmbSubAttribute.SelectedValue))
                        {
                            modelListSubAttribute.RemoveAt(i);
                        }
                    }
                    for (int i = 0; i < ListAddSelectAttribute.Count; i++)
                    {
                        if (ListAddSelectAttribute[i].SelectSubAttribute == selectSubAttribute && ListAddSelectAttribute[i].SelectAttribute == selectAttribute)
                        {
                            ListAddSelectAttribute.RemoveAt(i);
                        }
                    }
                    BDCS.Common.AddResult.selectAttributeText = string.Empty;
                    foreach (Model.AddSelectAttributes a in ListAddSelectAttribute)
                    {
                        BDCS.Common.AddResult.selectAttributeText = a.SelectSubAttribute + "," + a.SelectAttribute + ";";
                    }
                    lblSelectAttribute.Text = string.Empty;
                    lblSelectAttribute.Text += "选择的建筑类别查询条件为：" + BDCS.Common.AddResult.selectAttributeText;
            }
        }

        private void cmbSubDcriterion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSubDcriterion.DataSource != null)
            {
                try
                {
                    dstid = Convert.ToInt32(cmbSubDcriterion.SelectedValue);
                    CmbcmbBuildDcriterionContent(dstid);
                }
                catch (Exception ex)
                {
                    DataRowView drv = (DataRowView)cmbSubDcriterion.SelectedValue;
                    dstid = Convert.ToInt32(drv.Row["DSTID"]);
                }
            }
        }

        private void cmbBuildDcriterionContent_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbBuildDcriterionContent.DataSource != null)
            {
                try
                {
                    dcid = Convert.ToInt32(cmbBuildDcriterionContent.SelectedValue);
                    this.btnAddDcriterion.Enabled = true;
                    this.btnDcriterionCancel.Enabled = true;
                }
                catch (Exception ex)
                {
                    DataRowView drv = (DataRowView)cmbBuildDcriterionContent.SelectedValue;
                    dcid = Convert.ToInt32(drv.Row["DCID"]);
                    this.btnAddDcriterion.Enabled = true;
                    this.btnDcriterionCancel.Enabled = true;
                }
            }
            else
            {
                this.btnAddDcriterion.Enabled = false;
                this.btnDcriterionCancel.Enabled = false;
            }
        }

        private void btnAddDcriterion_Click(object sender, EventArgs e)
        {
            if (modelListBuildDesignDcriterionContent.Count != 0)
            {

                
                        
                    
                
            }
            else
            {
                modelBuildDesignDcriterionContent = new Model.T_BuildDesignDcriterionContents();
                bllBuildDesignDcriterionContent = new BLL.T_BuildDesignDcriterionContents();
                modelBuildDesignDcriterionContent = bllBuildDesignDcriterionContent.GetModel(dcid);
                modelListBuildDesignDcriterionContent.Add(modelBuildDesignDcriterionContent);
                listDstid.Add(dstid);
                listDcid.Add(dcid);
                selectCount++;
                isSelect++;
            }
        }

        private void btnDcriterionCancel_Click(object sender, EventArgs e)
        {
            if (modelListBuildDesignDcriterionContent.Count != 0)
            {
                if (modelListBuildDesignDcriterionContent.Count - 1 != 0)
                {
                    modelListBuildDesignDcriterionContent.Remove(modelListBuildDesignDcriterionContent[modelListBuildDesignDcriterionContent.Count - 1]);
                    selectCount--;
                }
                else
                {
                    modelListBuildDesignDcriterionContent.Remove(modelListBuildDesignDcriterionContent[0]);
                    selectCount--;
                    // isSelect = 0;
                }
            }
            else
            {
                MessageBox.Show("没有添加任何规范查询条件，无需取消！");
                //  isSelect = 0;
            }
        }

        //private void cmbDcriterion_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        dtidTwo = Convert.ToInt32(cmbDcriterion.SelectedValue);
        //    }
        //    catch (Exception ex)
        //    {
        //        DataRowView drv = (DataRowView)cmbDcriterion.SelectedValue;
        //        dtidTwo = Convert.ToInt32(drv.Row["DTID"]);
        //        CmbSubDcriterionTwoDataBinding(dtidTwo);
        //    }
        //}

        private void cmbSubDcriterionTwo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSubDcriterionTwo.DataSource != null)
            {
                try
                {
                    dstidTwo = Convert.ToInt32(cmbSubDcriterionTwo.SelectedValue);
                    // CmbcmbBuildDcriterionContent(dstidTwo);
                }
                catch (Exception ex)
                {
                    DataRowView drv = (DataRowView)cmbSubDcriterionTwo.SelectedValue;
                    dstidTwo = Convert.ToInt32(drv.Row["DSTID"]);
                }
            }
        }

        private void btnResultAdd_Click(object sender, EventArgs e)
        {
            listDstidTwo.Add(dstidTwo);
        }

        private void btnResultCancel_Click(object sender, EventArgs e)
        {
            if (listDstidTwo.Count != 0)
            {
                if (listDstidTwo.Count - 1 != 0)
                {
                    listDstidTwo.Remove(listDstidTwo[listDstidTwo.Count - 1]);
                }
                else
                {
                    listDstidTwo.Remove(listDstidTwo[0]);
                }
            }
            else
            {
                MessageBox.Show("没有选择任何查询结果参数，无需取消！");
            }
        }

        private void btnResultSelect_Click(object sender, EventArgs e)
        {
            if (isSelect != 0)
            {
                if (listSaid.Count != 0 && listSbaid.Count != 0 & listDstidTwo.Count != 0)
                {
                    dataSet = new DataSet();
                    dataSet = SelectResult(listSaid, listDcid, listDstidTwo);
                    if (dataSet == null)
                    {
                        MessageBox.Show("该查询条件下无相关规范结果，请重新设定后再进行查询！");
                    }
                }
                else
                {
                    MessageBox.Show("请选择查询的建筑属性参数条件，需要查询的规范结果项！");
                }
            }
            else
            {
                MessageBox.Show("请选择查询条件！");
            }
        }
    }
}

